package com.sls.web.manager.action.game;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GoodsBrand;
import com.sls.web.manager.game.querybean.GoodsBrandQuery;
import com.sls.web.manager.game.service.GoodsBrandService;
import com.sls.web.manager.page.Pagination;

public class GoodsBrandAction extends SuperAction {

	private static final long serialVersionUID = 4955114814468281923L;
	@Resource(name = "goodsBrandSerice")
	private GoodsBrandService goodsBrandService;
	private GoodsBrand goodsBrand;
	private String id;
	private String goodsCategoryId;
	private GoodsBrandQuery goodsBrandQuery;
	private Pagination<GoodsBrand> page;
	private String brandId;
	private String brandName;

	/**
	 * 添加
	 * 
	 * @return
	 */
	public String add() {
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			boolean result = goodsBrandService.add(goodsBrand,
					getCurrentOperator());
			if (!result) {
				return ERROR;
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		redirectionUrl = "goods_brand!list.htm";
		return SUCCESS;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		try {
			goodsBrand = goodsBrandService.queryGoodsBrandById(goodsCategoryId,
					getCurrentOperator());
			if (goodsBrand == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return INPUT;
		}
		return INPUT;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {

		try {
			boolean result = goodsBrandService.update(goodsBrand,
					getCurrentOperator());
			if (!result) {
				return ERROR;
			} else {
				if (goodsBrandQuery == null) {
					goodsBrandQuery = new GoodsBrandQuery();
					goodsBrandQuery.setPageSize(Constants.INIT_PAGESIZE);
					goodsBrandQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
				}
				if (StringUtils.isNotEmpty(brandId)) {
					goodsBrandQuery.setGoodsCategoryId(brandId);
				}
				if (StringUtils.isNotEmpty(brandName)) {
					goodsBrandQuery.setGameName(brandName);
				}
				return list();
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
			redirectionUrl = "goods_brand!list.htm";
			return SUCCESS;
		}
	}

	/**
	 * 详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception {
		try {
			goodsBrand = goodsBrandService.queryGoodsBrandById(goodsCategoryId,
					getCurrentOperator());
			if (goodsBrand == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			return ERROR;
		}
		return "detail";
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		try {
			goodsBrandService.delete(id, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}

		return list();
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		if (goodsBrandQuery == null) {
			goodsBrandQuery = new GoodsBrandQuery();
			goodsBrandQuery.setPageSize(Constants.INIT_PAGESIZE);
			goodsBrandQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			goodsBrandQuery.setIsDelete("1");
			page = goodsBrandService.queryGoodsBrand(goodsBrandQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 搜索后的列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String brandList() throws Exception {
		if (goodsBrandQuery == null) {
			goodsBrandQuery = new GoodsBrandQuery();
			goodsBrandQuery.setPageSize(Constants.INIT_PAGESIZE);
			goodsBrandQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			String goodsCategoryId = goodsBrandQuery.getGoodsCategoryId();
			String gameName = goodsBrandQuery.getGameName();
			goodsCategoryId = FilterStringSpaces
					.checkStringHasSpace(goodsCategoryId);
			gameName = FilterStringSpaces.checkStringHasSpace(gameName);
			goodsBrandQuery.setGoodsCategoryId(goodsCategoryId);
			goodsBrandQuery.setGameName(gameName);
			page = goodsBrandService.queryGoodsBrand(goodsBrandQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	public GoodsBrand getGoodsBrand() {
		return goodsBrand;
	}

	public void setGoodsBrand(GoodsBrand goodsBrand) {
		this.goodsBrand = goodsBrand;
	}

	public String getGoodsCategoryId() {
		return goodsCategoryId;
	}

	public void setGoodsCategoryId(String goodsCategoryId) {
		this.goodsCategoryId = goodsCategoryId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public GoodsBrandQuery getGoodsBrandQuery() {
		return goodsBrandQuery;
	}

	public void setGoodsBrandQuery(GoodsBrandQuery goodsBrandQuery) {
		this.goodsBrandQuery = goodsBrandQuery;
	}

	public Pagination<GoodsBrand> getPage() {
		return page;
	}

	public void setPage(Pagination<GoodsBrand> page) {
		this.page = page;
	}

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

}
